@extends('admin.layouts.master')
@section('content')
    <link rel="stylesheet"
          href="{{asset('static/bootstrap/css/bootstrap.min.css')}}">
    <table class="mdui-table mdui-table-hoverable">
        <thead>
        <tr>
            <th>ID</th>
            <th>权限的名称</th>
            <th>权限的路由</th>
            <th>描述</th>
            <th>是否为菜单</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody>
        @foreach($permissions as $permission)
            <tr>
                <td>{{$permission->id}}</td>
                <td>{{$permission->name}}</td>
                <td>{{str_repeat('-', 4*$permission->count).$permission->display_name}}</td>
                <td>
                    {{$permission->description}}
                </td>
                <td>
                    <label class="mdui-switch">
                        <input type="checkbox" is_menu="{{$permission->is_menu}}"
                               class="change" {{$permission->is_menu==1?'checked':''}}/>
                        <i class="mdui-switch-icon"></i>
                    </label>
                </td>
                <td>
                    <a href="{{url('/admin/admin_permission/edit',['id'=>$permission->id])}}"
                       class="icon-trash mdui-btn mdui-btn-dense mdui-color-blue mdui-ripple">修改</a>
                    {{--删除--}}
                    <a href="{{url('/admin/admin_permission/destroy',['id'=>$permission->id])}}"
                       class="icon-trash mdui-btn mdui-btn-dense mdui-color-blue mdui-ripple">删除</a>
                </td>
            </tr>
        @endforeach
        </tbody>
    </table>
    <div class="pull-right">
        {{$permissions->render()}}
    </div>
    <!-- jQuery 文件 -->
    <script src="{{asset('static/jquery/jquery.min.js')}}"></script>
    <!-- Bootstrap JavaScript 文件 -->
    <script src="{{asset('static/bootstrap/js/bootstrap.min.js')}}"></script>
@endsection
@section('js')
    <script>
        $(".change").click(function () {
            //获取每次选择得到的id
            var permissions_id = $(this).parent().parent().parent().find('td').first().text();
            //增加一个标识
            var is_menu = $(this).attr('is_menu');
            //指定选定的ID
            var mythis = $(this);
            $.ajax({
                url: '{{url('/admin/admin_permission/status')}}',
                dataType: 'json',
                data: {'is_menu': is_menu, 'permissions_id': permissions_id},
                success: function (data) {
                    if (data.message === 1) {
                        mythis.attr('checked', '');
                        mythis.attr('is_menu', '1');
                    } else {
                        mythis.removeAttr('checked');
                        mythis.attr('is_menu', '0');
                    }
                }
            });
        });
    </script>
    <script>
        $(function () {
            $('.icon-trash').click(function () {

                id = parseInt($(this).parent().parent().find('td:eq(0)').text());
                $.ajax({
                    url: '{{url('/admin/hasPermission')}}',
                    type: 'get',
                    data: {permission_id:id},
                    dataType: 'json',
                    async: false,
                    success: function (res) {
                        st = res;
                        if(st) {
                            alert('警告：该权限下还有子权限，无法修改或删除！');
                        }
                    }
                });
                if (st) {
                    return false;
                } else {
                    if (!confirm('警告：确定修改或者删除该权限吗？')) {
                        return false;
                    }
                }
            });
        });
    </script>

@endsection